class unionset
{
public:
    vector<int> fa;
    unionset(int n)
    {
        for (int i = 0; i <= n; i++) fa.push_back(i);
    }
    int find(int n)
    {
        return fa[n] == n ? n : fa[n] = find(fa[n]);
    }
    void merge(int a, int b)
    {
        fa[find(a)] = find(b);
    }
};


class Solution {
public:
    bool validPath(int n, vector<vector<int>>& edges, int source, int destination) {
        unionset s(n);
        for (int i = 0; i < edges.size(); i++)
        {
            int a = edges[i][0], b = edges[i][1];
            s.merge(a, b);
        }
        return s.find(source) == s.find(destination);
    }
};